//
//  Copyright 2015 XiaoJSoft Studio.
//  Copyright 2015 Harbin Institute of Technology(HIT), Software Engineering Department.
//
//  Use of this source code is governed by a proprietary license. You can not read, change or
//  redistribute this source code unless you have a written authorization from the copyright
//  holder listed above.
//

#include "uijs_compiler/public_api.h"
#include "sources/text_file.h"
#include <QDir>
#include <QFile>
#include <QTextStream>
#include <QIODevice>
#include <iostream>
#include <string>
#include <vector>
#include <string.h>

int main(int argc, char **argv) {
#define STATE_ROOT 0
#define STATE_SET_OUTPUT 1

    //  Initialize output files container.
    std::vector<std::string> output_files;
    output_files.clear();

    //  Initialize package base directory.
    std::string package_base = ".";

    //  Initialize package set flag.
    bool is_package_base_set = false;

    //  Initialize the state machine.
    int state = STATE_ROOT;

    for (int arg_id = 1; arg_id < argc; ++arg_id) {
        if (state == STATE_ROOT) {
            if (strcmp(argv[arg_id], "-o") == 0) {
                //  Go to read output file names.
                state = STATE_SET_OUTPUT;
            } else {
                //  Duplication check.
                if (is_package_base_set) {
                    std::cerr << "Error: Only 1 package path is allowed." << std::endl;
                    return 1;
                }

                //  Set package base directory and mark the flag.
                package_base = argv[arg_id];
                is_package_base_set = true;
            }
        } else if (state == STATE_SET_OUTPUT) {
            //  Add current argument to the output files list.
            output_files.push_back(std::string(argv[arg_id]));

            //  Go to root state.
            state = STATE_ROOT;
        } else {
            std::cerr << "Error: Program error." << std::endl;
            return 1;
        }
    }

    //  Check the syntax.
    if (state != STATE_ROOT) {
        std::cerr << "Error: Invalid arguments syntax." << std::endl;
        return 1;
    }

    //  If no output file specified, use "index.html" as default.
    if (output_files.size() == 0) {
        output_files.push_back(std::string("index.html"));
    }

    //  Generate the object of current directory.
    QDir current_dir = QDir(QString::fromStdString(package_base));
    if ((!current_dir.exists()) || (!current_dir.makeAbsolute())) {
        std::cerr << "Error: File system error." << std::endl;
        return 1;
    }

    //  Compile.
    std::string html;
    if (!ui::compiler::compileUI(current_dir.absolutePath().toStdString(), html)) {
        std::cerr << "Error: Compilation failed." << std::endl;
        return 1;
    }

    //  Write file.
    for (std::vector<std::string>::iterator file_name = output_files.begin(); file_name != output_files.end(); ++file_name) {
        if (!ui::compiler::textFileWrite(*file_name, html)) {
            std::cerr << "Error: Writing failed." << std::endl;
            return 1;
        }
    }

    //  Print success message.
    std::cout << "OK!" << std::endl;

#undef STATE_ROOT
#undef STATE_SET_OUTPUT

    return 0;
}

